package cn.exrick.xboot.modules.biddataproject.serviceimpl;


import cn.exrick.xboot.modules.biddataproject.entity.*;
import cn.exrick.xboot.modules.biddataproject.mapper.BidDataProtectMapper;
import cn.exrick.xboot.modules.biddataproject.service.IBidDataProtectService;
import cn.exrick.xboot.modules.projectinfo.entity.AllBoardData;
import cn.exrick.xboot.modules.projectinfo.entity.CompanyNumberAmount;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 招投标系统接口实现
 *
 * @author sdtf
 */
@Slf4j
@Service
@DS("other")
@Transactional
public class IBidDataProtectServiceImpl extends ServiceImpl<BidDataProtectMapper, BidDataProtect> implements IBidDataProtectService {

    @Autowired
    private BidDataProtectMapper bidDataProtectMapper;

    /**
     * 查找运营商年度相关数据
     */
    @Override
    public SelectData selectCountMoneyByOperator() {
        SelectData selectData = bidDataProtectMapper.selectCountMoneyByOperator();
        //数值转换，将元转化为万元
//        double contractFee = selectData.getContractFee() / 10000;
//        long longValue = Math.round(contractFee);
//        selectData.setContractFee(longValue);
        return selectData;
    }

    /**
     * 查找运营商月度相关数据
     */
    @Override
    public SelectData selectCountMoneyByOperatorByMonth() {
        SelectData selectData = bidDataProtectMapper.selectCountMoneyByOperatorByMonth();
        //数值转换，将元转化为万元
//        double contractFee = selectData.getContractFee() / 10000;
//        long longValue = Math.round(contractFee);
//        selectData.setContractFee(longValue);
        return selectData;
    }

    /**
     * 查找集客年度相关数据
     */
    @Override
    public SelectData selectCountMoneyByGroup() {
        SelectData selectData = bidDataProtectMapper.selectCountMoneyByGroup();
        if (selectData.getContractNO()==null){
            selectData.setContractNO(0);
        }
        if (selectData.getContractFee()==null){
            selectData.setContractFee(0L);
        }
        return selectData;
    }

    /**
     * 查找集客月度相关数据
     */
    @Override
    public SelectData selectCountMoneyByGroupByMonth() {
        SelectData selectData = bidDataProtectMapper.selectCountMoneyByGroupByMonth();
        if (selectData.getContractNO()==null){
            selectData.setContractNO(0);
        }
        if (selectData.getContractFee()==null){
            selectData.setContractFee(0L);
        }
        return selectData;
    }

    /**
     * 年度合同排行
     */
    @Override
    public List<SelectData> selectBidYear() {
        List<SelectData> selectData = bidDataProtectMapper.selectBidYear();
//        int order = 1;
//        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
//        for (SelectData selectDatum : selectData) {
//            //数值转换，将元转化为万元
//            double contractFee = selectDatum.getContractFee() / 10000.0;
//            long longValue = Math.round(contractFee);
//            selectDatum.setContractFee(longValue);
//            //根据部门id查询部门名称
//            String deptId = selectDatum.getDeptId();
//            String deptName = bidDataProtectMapper.selectDeptNameByDeptId(deptId);
//            selectDatum.setDeptName(deptName + "公司");
//            //设置排名
//            selectDatum.setOrder(order);
//            order++;
//        }
        return selectData;
    }

    /**
     * 本月合同排行
     */
    @Override
    public List<SelectData> selectBidMonth() {
        List<SelectData> selectData = bidDataProtectMapper.selectBidMonth();
//        int order = 1;
//        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
//        for (SelectData selectDatum : selectData) {
//            //数值转换，将元转化为万元
//            double contractFee = selectDatum.getContractFee() / 10000.0;
//            long longValue = Math.round(contractFee);
//            selectDatum.setContractFee(longValue);
////            String formattedContractFee = decimalFormat.format(contractFee);
////            selectDatum.setContractFee(Double.parseDouble(formattedContractFee));
//            //根据客户类型查询名称
//            String deptId = selectDatum.getDeptId();
//            String deptName = bidDataProtectMapper.selectDeptNameByDeptId(deptId);
//            if (deptName.endsWith("部") || deptName.endsWith("公司")) {
//                selectDatum.setDeptName(deptName);
//            } else {
//                selectDatum.setDeptName(deptName + "公司");
//            }
//            //设置排名
//            selectDatum.setOrder(order);
//            order++;
//        }
        return selectData;
    }

    /**
     * 最新中标项目
     */
    @Override
    public List<SelectData> selectNewBid() {
        List<SelectData> selectData = bidDataProtectMapper.selectNewBid();
        int order = 1;
        for (SelectData selectDatum : selectData) {
            //根据客户类型查询名称
            Integer customerType = selectDatum.getCustomerType();
            String customerName = bidDataProtectMapper.selectLabelByCustomerType(customerType);
            selectDatum.setCustomerName(customerName);
            //设置排名
            selectDatum.setOrder(order);
            order++;
        }
        return selectData;
    }

    /**
     * 经营单位合同签订情况
     */
    @Override
    public LinkedList<Object> selectBidYear1() {
//        部门名称集合
        LinkedList<String> deptList = new LinkedList<>();
//        合同金额集合
        LinkedList<Long> moneyList = new LinkedList<>();
//        返回数据集合
        LinkedList<Object> list = new LinkedList<>();
        List<SelectData> selectData = bidDataProtectMapper.selectBidYear1();
        if (selectData!=null){
            for (SelectData selectDatum : selectData) {
                deptList.add(selectDatum.getDeptName());
                moneyList.add(selectDatum.getContractFee());
            }
        }
        Object[] objects = deptList.toArray();
        Object[] objects1 = moneyList.toArray();
        list.add(objects);
        list.add(objects1);
        return list;
    }
//        int order = 1;
//        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
//        for (SelectData selectDatum : selectData) {
//            //数值转换，将元转化为万元
//            double contractFee = selectDatum.getContractFee() / 10000.0;
//            long longValue = Math.round(contractFee);
//            selectDatum.setContractFee(longValue);
////            String formattedContractFee = decimalFormat.format(contractFee);
////            selectDatum.setContractFee(Double.parseDouble(formattedContractFee));
////            将所有的合同金额放到集合之中
//            moneyList.add(selectDatum.getContractFee());
////            将集合转化为数组
//            Object[] moneyArray = moneyList.toArray();
//            //根据部门id查询部门名称
//            String deptId = selectDatum.getDeptId();
//            String deptName = bidDataProtectMapper.selectDeptNameByDeptId(deptId);
//            if (deptName.endsWith("部") || deptName.endsWith("公司")) {
//                selectDatum.setDeptName(deptName);
//            } else {
//                selectDatum.setDeptName(deptName + "公司");
//            }
////            将部门名称放到集合之中
//            deptList.add(selectDatum.getDeptName());
////            将集合转化为数组
//            Object[] deptArray = deptList.toArray();
//            //设置排名
//            selectDatum.setOrder(order);
//            order++;
//        }


    /**
     * 客户类型查询
     */
    @Override
    public LinkedList<Object> selectCustomerType() {
//        创建三个集合
//        合同数量集合
        LinkedList<Double> NumList = new LinkedList<>();
//        客户名称集合
        LinkedList<String> NameList = new LinkedList<>();
//        返回数据集合
        LinkedList<Object> list = new LinkedList<>();

//        查找出各个运营商的合同金额
        List<SelectData> list1 = bidDataProtectMapper.selectCustomerTypeByOperator();
        for (SelectData data : list1) {
            double contractFee = data.getContractFee() / 10000.0;
            BigDecimal decimalFee = BigDecimal.valueOf(contractFee);
            BigDecimal roundedFee = decimalFee.setScale(2, BigDecimal.ROUND_HALF_UP);
            double result = roundedFee.doubleValue();
            NumList.add(result);
            String customerTypeName = data.getCustomerTypeName();
            NameList.add(customerTypeName);
        }
//        查找出各个集客的合同金额
        List<SelectData> list2 = bidDataProtectMapper.selectCustomerTypeByGroup();
        Double sumcontract = 0.0;
        for (SelectData data : list2) {
            Long contractFee = data.getContractFee();
            sumcontract += contractFee;
        }
        double contractFee = sumcontract / 10000.0;
        BigDecimal decimalFee = BigDecimal.valueOf(contractFee);
        BigDecimal roundedFee = decimalFee.setScale(2, BigDecimal.ROUND_HALF_UP);
        double result = roundedFee.doubleValue();
        NumList.add(result);
        NameList.add("集客");
//        将集合转换为数组并放到集合里返回
        Object[] objects = NumList.toArray();
        Object[] objects1 = NameList.toArray();
        list.add(objects);
        list.add(objects1);
        return list;
    }

    /**
     * 二级表格查询：合同签订数量金额本年
     */
    @Override
    public LinkedList<Object> bidNumAndMoneyByYear() {
        //创建集合进行封装
        LinkedList<Object> list = new LinkedList<>();
        //查找全部单位名称
        List<TwoTableData> twoTableData0 = bidDataProtectMapper.getdept();
        //查找本年合同签订数量和金额
        List<TwoTableData> twoTableData = bidDataProtectMapper.bidNumAndMoneyByYear();
        //查找省内合同签订数量和金额
        List<TwoTableData> twoTableData1 = bidDataProtectMapper.bidNumAndMoneyByYearInProvince();
        //查找运营商本年合同签订数量和金额
        List<TwoTableData> twoTableData2 = bidDataProtectMapper.bidNumAndMoneyByYearByYYS();
        //查找运营商本年省内合同签订数量和金额
        List<TwoTableData> twoTableData3 = bidDataProtectMapper.bidNumAndMoneyByYearByYYSInProvince();
        //查找集客本年省内合同签订数量和金额
        List<TwoTableData> twoTableData4 = bidDataProtectMapper.bidNumAndMoneyByYearByJKInProvince();
        //查找集客本年省外合同签订数量和金额
        List<TwoTableData> twoTableData5 = bidDataProtectMapper.bidNumAndMoneyByYearByJKOutProvince();
        //循环进行set数据
        for (TwoTableData twoTableDatum : twoTableData) {
            //补充省内，省外合同签订数量，金额
            for (TwoTableData tableData : twoTableData1) {
                if (tableData.getDeptName().equals(twoTableDatum.getDeptName())) {
                    twoTableDatum.setContractNumberInProvince(tableData.getContractNumberInProvince());
                    twoTableDatum.setContractMoneyInProvince(tableData.getContractMoneyInProvince());
                    twoTableDatum.setContractNumberOutProvince(twoTableDatum.getContractNumber() - twoTableDatum.getContractNumberInProvince());
                    twoTableDatum.setContractMoneyOutProvince(twoTableDatum.getContractMoney() - twoTableDatum.getContractMoneyInProvince());
                }
            }
            //补充运营商，集客相关数据
            for (TwoTableData tableData : twoTableData2) {
                if (tableData.getDeptName().equals(twoTableDatum.getDeptName())) {
                    //运营商签订数量，金额
                    twoTableDatum.setContractNumberByYYS(tableData.getContractNumberByYYS());
                    twoTableDatum.setContractMoneyByYYS(tableData.getContractMoneyByYYS());
                    //集客签订数量，金额
                    twoTableDatum.setContractNumberByJK(twoTableDatum.getContractNumber() - twoTableDatum.getContractNumberByYYS());
                    twoTableDatum.setContractMoneyByJK(twoTableDatum.getContractMoney() - twoTableDatum.getContractMoneyByYYS());
                }
            }
            //补充运营商省内，省外数据
            for (TwoTableData tableData : twoTableData3) {
                if (tableData.getDeptName().equals(twoTableDatum.getDeptName())) {
                    //运营商签订数量，金额省内
                    twoTableDatum.setContractNumberByYYSInProvince(tableData.getContractNumberByYYSInProvince());
                    twoTableDatum.setContractMoneyByYYSInProvince(tableData.getContractMoneyByYYSInProvince());
                    //运营商签订数量，金额省外
                    twoTableDatum.setContractNumberByYYSOutProvince(twoTableDatum.getContractNumberByYYS() - twoTableDatum.getContractNumberByYYSInProvince());
                    twoTableDatum.setContractMoneyByYYSOutProvince(twoTableDatum.getContractMoneyByYYS() - twoTableDatum.getContractMoneyByYYSInProvince());
                }
            }
            //补充集客省内数据
            for (TwoTableData tableData : twoTableData4) {
                if (tableData.getDeptName().equals(twoTableDatum.getDeptName())) {
                    //集客签订数量，金额省内
                    twoTableDatum.setContractNumberByJKInProvince(tableData.getContractNumberByJKInProvince());
                    twoTableDatum.setContractMoneyByJKInProvince(tableData.getContractMoneyByJKInProvince());
                }
            }
            //补充集客省外数据
            for (TwoTableData tableData : twoTableData5) {
                if (tableData.getDeptName().equals(twoTableDatum.getDeptName())) {
                    //集客签订数量，金额省外
                    twoTableDatum.setContractNumberByJKOutProvince(tableData.getContractNumberByJKOutProvince());
                    twoTableDatum.setContractMoneyByJKOutProvince(tableData.getContractMoneyByJKOutProvince());
                }
            }
//            //封装数据
//            if (twoTableDatum.getDeptName().length() == 2) {
//                twoTableDatum.setDeptName(twoTableDatum.getDeptName() + "公司");
//            }
            //年度总数量
            if (twoTableDatum.getContractNumber() == null) {
                twoTableDatum.setContractNumber(0);
            }
            //年度总金额
            if (twoTableDatum.getContractMoney() == null) {
                twoTableDatum.setContractMoney(0L);
            }
            //年度总数量省内
            if (twoTableDatum.getContractNumberInProvince() == null) {
                twoTableDatum.setContractNumberInProvince(0);
            }
            //年度总数量省外
            if (twoTableDatum.getContractNumberOutProvince() == null) {
                twoTableDatum.setContractNumberOutProvince(0);
            }
            //年度总金额省内
            if (twoTableDatum.getContractMoneyInProvince() == null) {
                twoTableDatum.setContractMoneyInProvince(0L);
            }
            //年度总金额省外
            if (twoTableDatum.getContractMoneyOutProvince() == null) {
                twoTableDatum.setContractMoneyOutProvince(0L);
            }
            //运营商总数量
            if (twoTableDatum.getContractNumberByYYS() == null) {
                twoTableDatum.setContractNumberByYYS(0);
            }
            //运营商总金额
            if (twoTableDatum.getContractMoneyByYYS() == null) {
                twoTableDatum.setContractMoneyByYYS(0L);
            }
            //运营商总数量省内
            if (twoTableDatum.getContractNumberByYYSInProvince() == null) {
                twoTableDatum.setContractNumberByYYSInProvince(0);
            }
            //运营商总数量省外
            if (twoTableDatum.getContractNumberByYYSOutProvince() == null) {
                twoTableDatum.setContractNumberByYYSOutProvince(0);
            }
            //运营商总金额省内
            if (twoTableDatum.getContractMoneyByYYSInProvince() == null) {
                twoTableDatum.setContractMoneyByYYSInProvince(0L);
            }
            //运营商总金额省外
            if (twoTableDatum.getContractMoneyByYYSOutProvince() == null) {
                twoTableDatum.setContractMoneyByYYSOutProvince(0L);
            }
            //集客总数量
            if (twoTableDatum.getContractNumberByJK() == null) {
                twoTableDatum.setContractNumberByJK(0);
            }
            //集客总金额
            if (twoTableDatum.getContractMoneyByJK() == null) {
                twoTableDatum.setContractMoneyByJK(0L);
            }
            //集客总数量省内
            if (twoTableDatum.getContractNumberByJKInProvince() == null) {
                twoTableDatum.setContractNumberByJKInProvince(0);
            }
            //集客总金额省内
            if (twoTableDatum.getContractMoneyByJKInProvince() == null) {
                twoTableDatum.setContractMoneyByJKInProvince(0L);
            }
            //集客总数量省外
            if (twoTableDatum.getContractNumberByJKOutProvince() == null) {
                twoTableDatum.setContractNumberByJKOutProvince(0);
            }
            //集客总金额省外
            if (twoTableDatum.getContractMoneyByJKOutProvince() == null) {
                twoTableDatum.setContractMoneyByJKOutProvince(0L);
            }
            list.add(twoTableDatum);
        }
        return list;
    }

    /**
     * 二级表格查询：合同签订数量金额本月
     */
    @Override
    public LinkedList<Object> bidNumAndMoneyByMonth() {
        //创建集合进行封装
        LinkedList<Object> list = new LinkedList<>();
        //查找本年合同签订数量和金额
        List<TwoTableData> twoTableData = bidDataProtectMapper.bidNumAndMoneyByMonth();
        //查找省内合同签订数量和金额
        List<TwoTableData> twoTableData1 = bidDataProtectMapper.bidNumAndMoneyByYearInProvinceByMonth();
        //查找运营商本年合同签订数量和金额
        List<TwoTableData> twoTableData2 = bidDataProtectMapper.bidNumAndMoneyByYearByYYSByMonth();
        //查找运营商本年省内合同签订数量和金额
        List<TwoTableData> twoTableData3 = bidDataProtectMapper.bidNumAndMoneyByYearByYYSInProvinceByMonth();
        //查找集客本年省内合同签订数量和金额
        List<TwoTableData> twoTableData4 = bidDataProtectMapper.bidNumAndMoneyByYearByJKInProvinceByMonth();
        //查找集客本年省外合同签订数量和金额
        List<TwoTableData> twoTableData5 = bidDataProtectMapper.bidNumAndMoneyByYearByJKOutProvinceByMonth();
        //循环进行set数据
        for (TwoTableData twoTableDatum : twoTableData) {
            //补充省内，省外合同签订数量，金额
            for (TwoTableData tableData : twoTableData1) {
                if (tableData.getDeptName().equals(twoTableDatum.getDeptName())) {
                    twoTableDatum.setContractNumberInProvince(tableData.getContractNumberInProvince());
                    twoTableDatum.setContractMoneyInProvince(tableData.getContractMoneyInProvince());
                    twoTableDatum.setContractNumberOutProvince(twoTableDatum.getContractNumber() - twoTableDatum.getContractNumberInProvince());
                    twoTableDatum.setContractMoneyOutProvince(twoTableDatum.getContractMoney() - twoTableDatum.getContractMoneyInProvince());
                }
            }
            //补充运营商，集客相关数据
            for (TwoTableData tableData : twoTableData2) {
                if (tableData.getDeptName().equals(twoTableDatum.getDeptName())) {
                    //运营商签订数量，金额
                    twoTableDatum.setContractNumberByYYS(tableData.getContractNumberByYYS());
                    twoTableDatum.setContractMoneyByYYS(tableData.getContractMoneyByYYS());
                    //集客签订数量，金额
                    twoTableDatum.setContractNumberByJK(twoTableDatum.getContractNumber() - twoTableDatum.getContractNumberByYYS());
                    twoTableDatum.setContractMoneyByJK(twoTableDatum.getContractMoney() - twoTableDatum.getContractMoneyByYYS());
                }
            }
            //补充运营商省内，省外数据
            for (TwoTableData tableData : twoTableData3) {
                if (tableData.getDeptName().equals(twoTableDatum.getDeptName())) {
                    //运营商签订数量，金额省内
                    twoTableDatum.setContractNumberByYYSInProvince(tableData.getContractNumberByYYSInProvince());
                    twoTableDatum.setContractMoneyByYYSInProvince(tableData.getContractMoneyByYYSInProvince());
                    //运营商签订数量，金额省外
                    twoTableDatum.setContractNumberByYYSOutProvince(twoTableDatum.getContractNumberByYYS() - twoTableDatum.getContractNumberByYYSInProvince());
                    twoTableDatum.setContractMoneyByYYSOutProvince(twoTableDatum.getContractMoneyByYYS() - twoTableDatum.getContractMoneyByYYSInProvince());
                }
            }
            //补充集客省内数据
            for (TwoTableData tableData : twoTableData4) {
                if (tableData.getDeptName().equals(twoTableDatum.getDeptName())) {
                    //集客签订数量，金额省内
                    twoTableDatum.setContractNumberByJKInProvince(tableData.getContractNumberByJKInProvince());
                    twoTableDatum.setContractMoneyByJKInProvince(tableData.getContractMoneyByJKInProvince());
                }
            }
            //补充集客省外数据
            for (TwoTableData tableData : twoTableData5) {
                if (tableData.getDeptName().equals(twoTableDatum.getDeptName())) {
                    //集客签订数量，金额省外
                    twoTableDatum.setContractNumberByJKOutProvince(tableData.getContractNumberByJKOutProvince());
                    twoTableDatum.setContractMoneyByJKOutProvince(tableData.getContractMoneyByJKOutProvince());
                }
            }
            //封装数据
            //年度总数量
            if (twoTableDatum.getContractNumber() == null) {
                twoTableDatum.setContractNumber(0);
            }
            //年度总金额
            if (twoTableDatum.getContractMoney() == null) {
                twoTableDatum.setContractMoney(0L);
            }
            //年度总数量省内
            if (twoTableDatum.getContractNumberInProvince() == null) {
                twoTableDatum.setContractNumberInProvince(0);
            }
            //年度总数量省外
            if (twoTableDatum.getContractNumberOutProvince() == null) {
                twoTableDatum.setContractNumberOutProvince(0);
            }
            //年度总金额省内
            if (twoTableDatum.getContractMoneyInProvince() == null) {
                twoTableDatum.setContractMoneyInProvince(0L);
            }
            //年度总金额省外
            if (twoTableDatum.getContractMoneyOutProvince() == null) {
                twoTableDatum.setContractMoneyOutProvince(0L);
            }
            //运营商总数量
            if (twoTableDatum.getContractNumberByYYS() == null) {
                twoTableDatum.setContractNumberByYYS(0);
            }
            //运营商总金额
            if (twoTableDatum.getContractMoneyByYYS() == null) {
                twoTableDatum.setContractMoneyByYYS(0L);
            }
            //运营商总数量省内
            if (twoTableDatum.getContractNumberByYYSInProvince() == null) {
                twoTableDatum.setContractNumberByYYSInProvince(0);
            }
            //运营商总数量省外
            if (twoTableDatum.getContractNumberByYYSOutProvince() == null) {
                twoTableDatum.setContractNumberByYYSOutProvince(0);
            }
            //运营商总金额省内
            if (twoTableDatum.getContractMoneyByYYSInProvince() == null) {
                twoTableDatum.setContractMoneyByYYSInProvince(0L);
            }
            //运营商总金额省外
            if (twoTableDatum.getContractMoneyByYYSOutProvince() == null) {
                twoTableDatum.setContractMoneyByYYSOutProvince(0L);
            }
            //集客总数量
            if (twoTableDatum.getContractNumberByJK() == null) {
                twoTableDatum.setContractNumberByJK(0);
            }
            //集客总金额
            if (twoTableDatum.getContractMoneyByJK() == null) {
                twoTableDatum.setContractMoneyByJK(0L);
            }
            //集客总数量省内
            if (twoTableDatum.getContractNumberByJKInProvince() == null) {
                twoTableDatum.setContractNumberByJKInProvince(0);
            }
            //集客总金额省内
            if (twoTableDatum.getContractMoneyByJKInProvince() == null) {
                twoTableDatum.setContractMoneyByJKInProvince(0L);
            }
            //集客总数量省外
            if (twoTableDatum.getContractNumberByJKOutProvince() == null) {
                twoTableDatum.setContractNumberByJKOutProvince(0);
            }
            //集客总金额省外
            if (twoTableDatum.getContractMoneyByJKOutProvince() == null) {
                twoTableDatum.setContractMoneyByJKOutProvince(0L);
            }
            list.add(twoTableDatum);
        }
        return list;
    }

    @Override
    public SelectData seleteNumberMoneyByYear() {
       SelectData selectData =  bidDataProtectMapper.seleteNumberMoneyByYear();
        return selectData;
    }

    @Override
    public SelectData seleteNumberMoneyByMonth() {
        SelectData selectData =  bidDataProtectMapper.seleteNumberMoneyByMonth();
        return selectData;
    }

    @Override
    public List<AllBoardData> SYData() {
        return null;
    }

    @Override
    public List<ProjectDeptList> getProjectDeptlist() {

        List<ProjectDeptList> list = bidDataProtectMapper.getProjectDeptlist();
        return list;
    }

    @Override
    public List<BidDataProtectTable> getAnalysis() {

        List<BidDataProtectTable> list = new ArrayList<>();

        List<ProjectDeptList> bidList = bidDataProtectMapper.getBidList();

        //总体
        String[] overallArray = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14"};
        List<String> overallArrayList = new ArrayList<>(Arrays.asList(overallArray));
        List<ProjectDeptList> overallList = bidDataProtectMapper.getAnalysis(overallArrayList);
        //集客
        String[] GatheringCustomersArray = {"1","2","3","4","5","6","7","8","9"};
        List<String> gatheringCustomersList = new ArrayList<>(Arrays.asList(GatheringCustomersArray));
        List<ProjectDeptList> gatheringList = bidDataProtectMapper.getAnalysis(gatheringCustomersList);
        //运营商
        String[] OperatorArray = {"10","11","12","13","14"};
        List<String> operatorArrayList = new ArrayList<>(Arrays.asList(OperatorArray));
        List<ProjectDeptList> operatorList = bidDataProtectMapper.getAnalysis(operatorArrayList);
        //广电
        String[] gdArray = {"10"};
        List<String> gdArrayList = new ArrayList<>(Arrays.asList(gdArray));
        List<ProjectDeptList> gdList = bidDataProtectMapper.getAnalysis(gdArrayList);
        //铁塔
        String[] ttArray = {"11"};
        List<String> ttArrayList = new ArrayList<>(Arrays.asList(ttArray));
        List<ProjectDeptList> ttList = bidDataProtectMapper.getAnalysis(ttArrayList);
        //联通
        String[] ltArray = {"12"};
        List<String> ltArrayList = new ArrayList<>(Arrays.asList(ltArray));
        List<ProjectDeptList> ltList = bidDataProtectMapper.getAnalysis(ltArrayList);
        //移动
        String[] ydArray = {"13"};
        List<String> ydArrayList = new ArrayList<>(Arrays.asList(ydArray));
        List<ProjectDeptList> ydList = bidDataProtectMapper.getAnalysis(ydArrayList);
        //电信
        String[] dxArray = {"14"};
        List<String> dxArrayList = new ArrayList<>(Arrays.asList(dxArray));
        List<ProjectDeptList> dxList = bidDataProtectMapper.getAnalysis(dxArrayList);

        int num1 = 0;
        Long amount1 = 0L;
        int num2 = 0;
        Long amount2 = 0L;
        int num3 = 0;
        Long amount3 = 0L;
        int num4 = 0;
        Long amount4 = 0L;
        int num5 = 0;
        Long amount5 = 0L;
        int num6 = 0;
        Long amount6 = 0L;
        int num7 = 0;
        Long amount7 = 0L;
        int num8 = 0;
        Long amount8 = 0L;


        for(ProjectDeptList bid : bidList){
            BidDataProtectTable bidDataProtectTable = new BidDataProtectTable();
            bidDataProtectTable.setBidName(bid.getBidName());

            String bidCode = bid.getBidCode();
            if(overallList.size() > 0){
                for(ProjectDeptList g : overallList){
                    if(g.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num1 = num1 + g.getNum();
                            amount1 = amount1 + g.getSum();
                        }else {
                            bidDataProtectTable.setOverallNum(g.getNum());
                            bidDataProtectTable.setOverallAmount(g.getSum());
                        }

                    }
                }
            }
            if(gatheringList.size() > 0){
                for(ProjectDeptList g : gatheringList){
                    if(g.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num2 = num2 + g.getNum();
                            amount2 = amount2 + g.getSum();
                        }else {
                            bidDataProtectTable.setGatheringCustomersNum(g.getNum());
                            bidDataProtectTable.setGatheringCustomersAmount(g.getSum());
                        }

                    }
                }
            }
            if(operatorList.size() > 0){
                for(ProjectDeptList o : operatorList){
                    if(o.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num3 = num3 + o.getNum();
                            amount3 = amount3 + o.getSum();
                        }else {
                            bidDataProtectTable.setOperatorNum(o.getNum());
                            bidDataProtectTable.setOperatorAmount(o.getSum());
                        }

                    }
                }
            }
            if(gdList.size() > 0){
                for(ProjectDeptList gd : gdList){
                    if(gd.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num4 = num4 + gd.getNum();
                            amount4 = amount4 + gd.getSum();
                        }else {
                            bidDataProtectTable.setGdNum(gd.getNum());
                            bidDataProtectTable.setGdAmount(gd.getSum());
                        }

                    }
                }
            }
            if(ttList.size() > 0){
                for(ProjectDeptList tt : ttList){
                    if(tt.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num5 = num5 + tt.getNum();
                            amount5 = amount5 + tt.getSum();
                        }else {
                            bidDataProtectTable.setTtNum(tt.getNum());
                            bidDataProtectTable.setTtAmount(tt.getSum());
                        }

                    }
                }
            }
            if(ltList.size() > 0){
                for(ProjectDeptList lt : ltList){
                    if(lt.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num6 = num6 + lt.getNum();
                            amount6 = amount6 + lt.getSum();
                        }else {
                            bidDataProtectTable.setLtNum(lt.getNum());
                            bidDataProtectTable.setLtAmount(lt.getSum());
                        }

                    }
                }
            }
            if(ydList.size() > 0){
                for(ProjectDeptList yd : ydList){
                    if(yd.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num7 = num7 + yd.getNum();
                            amount7 = amount7 + yd.getSum();
                        }else {
                            bidDataProtectTable.setYdNum(yd.getNum());
                            bidDataProtectTable.setYdAmount(yd.getSum());
                        }

                    }
                }
            }
            if(dxList.size() > 0){
                for(ProjectDeptList dx : dxList){
                    if(dx.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num8 = num8 + dx.getNum();
                            amount8 = amount8 + dx.getSum();
                        }else {
                            bidDataProtectTable.setDxNum(dx.getNum());
                            bidDataProtectTable.setDxAmount(dx.getSum());
                        }

                    }
                }
            }



            Field[] fields = bidDataProtectTable.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    // 判断属性值是否为null
                    if (field.get(bidDataProtectTable) == null) {
                        // 设置默认值
                        if (field.getType() == Integer.class) {
                            field.set(bidDataProtectTable, 0);
                        }
                        if (field.getType() == Long.class) {
                            field.set(bidDataProtectTable, 0L);
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            if(!bidDataProtectTable.getBidName().equals("公司本部")){
                list.add(bidDataProtectTable);
            }


        }

        BidDataProtectTable b = new BidDataProtectTable();
        b.setBidName("公司本部");
        b.setOverallNum(num1);
        b.setOverallAmount(amount1);
        b.setGatheringCustomersNum(num2);
        b.setGatheringCustomersAmount(amount2);
        b.setOperatorNum(num3);
        b.setOperatorAmount(amount3);
        b.setGdNum(num4);
        b.setGdAmount(amount4);
        b.setTtNum(num5);
        b.setTtAmount(amount5);
        b.setLtNum(num6);
        b.setLtAmount(amount6);
        b.setYdNum(num7);
        b.setYdAmount(amount7);
        b.setDxNum(num8);
        b.setDxAmount(amount8);

        list.add(b);

        return list;
    }

    @Override
    public List<BidDataProtectTable> getAnalysisMonth(String str) {

        List<BidDataProtectTable> list = new ArrayList<>();

        List<ProjectDeptList> bidList = bidDataProtectMapper.getBidList();

        //总体
        String[] overallArray = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14"};
        List<String> overallArrayList = new ArrayList<>(Arrays.asList(overallArray));
        List<ProjectDeptList> overallList = bidDataProtectMapper.getAnalysisMonth(overallArrayList);
        //集客
        String[] GatheringCustomersArray = {"1","2","3","4","5","6","7","8","9"};
        List<String> gatheringCustomersList = new ArrayList<>(Arrays.asList(GatheringCustomersArray));
        List<ProjectDeptList> gatheringList = bidDataProtectMapper.getAnalysisMonth(gatheringCustomersList);
        //运营商
        String[] OperatorArray = {"10","11","12","13","14"};
        List<String> operatorArrayList = new ArrayList<>(Arrays.asList(OperatorArray));
        List<ProjectDeptList> operatorList = bidDataProtectMapper.getAnalysisMonth(operatorArrayList);
        //广电
        String[] gdArray = {"10"};
        List<String> gdArrayList = new ArrayList<>(Arrays.asList(gdArray));
        List<ProjectDeptList> gdList = bidDataProtectMapper.getAnalysisMonth(gdArrayList);
        //铁塔
        String[] ttArray = {"11"};
        List<String> ttArrayList = new ArrayList<>(Arrays.asList(ttArray));
        List<ProjectDeptList> ttList = bidDataProtectMapper.getAnalysisMonth(ttArrayList);
        //联通
        String[] ltArray = {"12"};
        List<String> ltArrayList = new ArrayList<>(Arrays.asList(ltArray));
        List<ProjectDeptList> ltList = bidDataProtectMapper.getAnalysisMonth(ltArrayList);
        //移动
        String[] ydArray = {"13"};
        List<String> ydArrayList = new ArrayList<>(Arrays.asList(ydArray));
        List<ProjectDeptList> ydList = bidDataProtectMapper.getAnalysisMonth(ydArrayList);
        //电信
        String[] dxArray = {"14"};
        List<String> dxArrayList = new ArrayList<>(Arrays.asList(dxArray));
        List<ProjectDeptList> dxList = bidDataProtectMapper.getAnalysisMonth(dxArrayList);

        int num1 = 0;
        Long amount1 = 0L;
        int num2 = 0;
        Long amount2 = 0L;
        int num3 = 0;
        Long amount3 = 0L;
        int num4 = 0;
        Long amount4 = 0L;
        int num5 = 0;
        Long amount5 = 0L;
        int num6 = 0;
        Long amount6 = 0L;
        int num7 = 0;
        Long amount7 = 0L;
        int num8 = 0;
        Long amount8 = 0L;


        for(ProjectDeptList bid : bidList){
            BidDataProtectTable bidDataProtectTable = new BidDataProtectTable();
            bidDataProtectTable.setBidName(bid.getBidName());

            String bidCode = bid.getBidCode();
            if(overallList.size() > 0){
                for(ProjectDeptList g : overallList){
                    if(g.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num1 = num1 + g.getNum();
                            amount1 = amount1 + g.getSum();
                        }else {
                            bidDataProtectTable.setOverallNum(g.getNum());
                            bidDataProtectTable.setOverallAmount(g.getSum());
                        }

                    }
                }
            }
            if(gatheringList.size() > 0){
                for(ProjectDeptList g : gatheringList){
                    if(g.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num2 = num2 + g.getNum();
                            amount2 = amount2 + g.getSum();
                        }else {
                            bidDataProtectTable.setGatheringCustomersNum(g.getNum());
                            bidDataProtectTable.setGatheringCustomersAmount(g.getSum());
                        }

                    }
                }
            }
            if(operatorList.size() > 0){
                for(ProjectDeptList o : operatorList){
                    if(o.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num3 = num3 + o.getNum();
                            amount3 = amount3 + o.getSum();
                        }else {
                            bidDataProtectTable.setOperatorNum(o.getNum());
                            bidDataProtectTable.setOperatorAmount(o.getSum());
                        }

                    }
                }
            }
            if(gdList.size() > 0){
                for(ProjectDeptList gd : gdList){
                    if(gd.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num4 = num4 + gd.getNum();
                            amount4 = amount4 + gd.getSum();
                        }else {
                            bidDataProtectTable.setGdNum(gd.getNum());
                            bidDataProtectTable.setGdAmount(gd.getSum());
                        }

                    }
                }
            }
            if(ttList.size() > 0){
                for(ProjectDeptList tt : ttList){
                    if(tt.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num5 = num5 + tt.getNum();
                            amount5 = amount5 + tt.getSum();
                        }else {
                            bidDataProtectTable.setTtNum(tt.getNum());
                            bidDataProtectTable.setTtAmount(tt.getSum());
                        }

                    }
                }
            }
            if(ltList.size() > 0){
                for(ProjectDeptList lt : ltList){
                    if(lt.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num6 = num6 + lt.getNum();
                            amount6 = amount6 + lt.getSum();
                        }else {
                            bidDataProtectTable.setLtNum(lt.getNum());
                            bidDataProtectTable.setLtAmount(lt.getSum());
                        }

                    }
                }
            }
            if(ydList.size() > 0){
                for(ProjectDeptList yd : ydList){
                    if(yd.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num7 = num7 + yd.getNum();
                            amount7 = amount7 + yd.getSum();
                        }else {
                            bidDataProtectTable.setYdNum(yd.getNum());
                            bidDataProtectTable.setYdAmount(yd.getSum());
                        }

                    }
                }
            }
            if(dxList.size() > 0){
                for(ProjectDeptList dx : dxList){
                    if(dx.getBidCode().equals(bidCode)){
                        if(bid.getBidName().equals("公司本部")){
                            num8 = num8 + dx.getNum();
                            amount8 = amount8 + dx.getSum();
                        }else {
                            bidDataProtectTable.setDxNum(dx.getNum());
                            bidDataProtectTable.setDxAmount(dx.getSum());
                        }

                    }
                }
            }



            Field[] fields = bidDataProtectTable.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    // 判断属性值是否为null
                    if (field.get(bidDataProtectTable) == null) {
                        // 设置默认值
                        if (field.getType() == Integer.class) {
                            field.set(bidDataProtectTable, 0);
                        }
                        if (field.getType() == Long.class) {
                            field.set(bidDataProtectTable, 0L);
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            if(!bidDataProtectTable.getBidName().equals("公司本部")){
                list.add(bidDataProtectTable);
            }


        }

        BidDataProtectTable b = new BidDataProtectTable();
        b.setBidName("公司本部");
        b.setOverallNum(num1);
        b.setOverallAmount(amount1);
        b.setGatheringCustomersNum(num2);
        b.setGatheringCustomersAmount(amount2);
        b.setOperatorNum(num3);
        b.setOperatorAmount(amount3);
        b.setGdNum(num4);
        b.setGdAmount(amount4);
        b.setTtNum(num5);
        b.setTtAmount(amount5);
        b.setLtNum(num6);
        b.setLtAmount(amount6);
        b.setYdNum(num7);
        b.setYdAmount(amount7);
        b.setDxNum(num8);
        b.setDxAmount(amount8);

        list.add(b);

        return list;
    }

    @Override
    public List<BidDataProtectTable> getAnalysisYears() {

        List<BidDataProtectTable> list = new ArrayList<>();

        //总体
        String[] overallArray = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14"};
        List<String> overallArrayList = new ArrayList<>(Arrays.asList(overallArray));
        List<ProjectDeptList> overalllist = bidDataProtectMapper.getAnalysisYears(overallArrayList,0);//今年
        List<ProjectDeptList> overalllist1 = bidDataProtectMapper.getAnalysisYears(overallArrayList,1);//去年
        List<ProjectDeptList> overalllist2 = bidDataProtectMapper.getAnalysisYears(overallArrayList,2);//前年
        //集客
        String[] GatheringCustomersArray = {"1","2","3","4","5","6","7","8","9"};
        List<String> gatheringCustomersList = new ArrayList<>(Arrays.asList(GatheringCustomersArray));
        List<ProjectDeptList> gatheringlist = bidDataProtectMapper.getAnalysisYears(gatheringCustomersList,0);
        List<ProjectDeptList> gatheringlist1 = bidDataProtectMapper.getAnalysisYears(gatheringCustomersList,1);
        List<ProjectDeptList> gatheringlist2 = bidDataProtectMapper.getAnalysisYears(gatheringCustomersList,2);
        //运营商
        String[] OperatorArray = {"10","11","12","13","14"};
        List<String> operatorArrayList = new ArrayList<>(Arrays.asList(OperatorArray));
        List<ProjectDeptList> operatorlist = bidDataProtectMapper.getAnalysisYears(operatorArrayList,0);
        List<ProjectDeptList> operatorlist1 = bidDataProtectMapper.getAnalysisYears(operatorArrayList,1);
        List<ProjectDeptList> operatorlist2 = bidDataProtectMapper.getAnalysisYears(operatorArrayList,2);

        List<ProjectDeptList> bidList = bidDataProtectMapper.getBidList1();

        for(ProjectDeptList bid : bidList){
            BidDataProtectTable bidDataProtectTable = new BidDataProtectTable();
            String bidName = bid.getBidName();
            bidDataProtectTable.setBidName(bidName);

            for(ProjectDeptList o : overalllist){
                if(o.getBidName().equals(bidName)){
                    bidDataProtectTable.setOverallNum(o.getNum());
                    bidDataProtectTable.setOverallAmount(o.getSum());
                }
            }
            for(ProjectDeptList o1 : overalllist1){
                if(o1.getBidName().equals(bidName)){
                    bidDataProtectTable.setOverallNum1(o1.getNum());
                    bidDataProtectTable.setOverallAmount1(o1.getSum());
                }
            }
            for(ProjectDeptList o2 : overalllist2){
                if(o2.getBidName().equals(bidName)){
                    bidDataProtectTable.setOverallNum2(o2.getNum());
                    bidDataProtectTable.setOverallAmount2(o2.getSum());
                }
            }
            for(ProjectDeptList g : gatheringlist){
                if(g.getBidName().equals(bidName)){
                    bidDataProtectTable.setGatheringCustomersNum(g.getNum());
                    bidDataProtectTable.setGatheringCustomersAmount(g.getSum());
                }
            }
            for(ProjectDeptList g1 : gatheringlist1){
                if(g1.getBidName().equals(bidName)){
                    bidDataProtectTable.setGatheringCustomersNum1(g1.getNum());
                    bidDataProtectTable.setGatheringCustomersAmount1(g1.getSum());
                }
            }
            for(ProjectDeptList g2 : gatheringlist2){
                if(g2.getBidName().equals(bidName)){
                    bidDataProtectTable.setGatheringCustomersNum2(g2.getNum());
                    bidDataProtectTable.setGatheringCustomersAmount2(g2.getSum());
                }
            }
            for(ProjectDeptList op : operatorlist){
                if(op.getBidName().equals(bidName)){
                    bidDataProtectTable.setOperatorNum(op.getNum());
                    bidDataProtectTable.setOperatorAmount(op.getSum());
                }
            }
            for(ProjectDeptList op1 : operatorlist1){
                if(op1.getBidName().equals(bidName)){
                    bidDataProtectTable.setOperatorNum1(op1.getNum());
                    bidDataProtectTable.setOperatorAmount1(op1.getSum());
                }
            }
            for(ProjectDeptList op2 : operatorlist2){
                if(op2.getBidName().equals(bidName)){
                    bidDataProtectTable.setOperatorNum2(op2.getNum());
                    bidDataProtectTable.setOperatorAmount2(op2.getSum());
                }
            }

            Field[] fields = bidDataProtectTable.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    // 判断属性值是否为null
                    if (field.get(bidDataProtectTable) == null) {
                        // 设置默认值
                        if (field.getType() == Integer.class) {
                            field.set(bidDataProtectTable, 0);
                        }
                        if (field.getType() == Long.class) {
                            field.set(bidDataProtectTable, 0L);
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            list.add(bidDataProtectTable);

        }


        return list;
    }

    @Override
    public List<ProjectDeptList> getCustomerRanking(String[] customerTypeArray,String month) {

        List<String> list = new ArrayList<>();

        if(customerTypeArray == null || customerTypeArray[0].equals("false")){
            for(int j = 1; j < 15; j++){
                String s = Integer.toString(j);
                list.add(s);
            }
        }else {
            list = new ArrayList<>(Arrays.asList(customerTypeArray));
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).equals("集客")) {
                    for(int j = 1; j < 10; j++){
                        String s = Integer.toString(j);
                        list.add(s);
                    }
                }
            }
        }

        List<ProjectDeptList> customerRanking = new ArrayList<>();

        if(month.equals("month")){
            customerRanking = bidDataProtectMapper.getCustomerRankingMonth(list);
        }else {
            customerRanking = bidDataProtectMapper.getCustomerRanking(list);
        }


        return customerRanking;
    }


}


